home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / p1123mz4.zip / X1123X3D.CPP < prev    next >
C/C++ Source or Header  |  1994-03-02  |  6KB  |  175 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <iostream.h>
  6. #include "titillat.h"
  7. #include "plot3d.h"
  8. #include "x1123x3d.h"
  9.  
  10. #ifndef TRUE
  11. #define TRUE -1
  12. #endif
  13. #ifndef FALSE
  14. #define FALSE 0
  15. #endif
  16.  
  17. static int pattern [8] [8]
  18.             = { { 0, 32,  8, 40,  2, 34, 10, 42},
  19.                 {48, 16, 56, 24, 50, 18, 58, 26},
  20.                 {12, 44,  4, 36, 14, 46,  6, 38},
  21.                 {60, 28, 52, 20, 62, 30, 54, 22},
  22.                 { 3, 35, 11, 43,  1, 33,  9, 41},
  23.                 {51, 19, 59, 27, 49, 17, 57, 25},
  24.                 {15, 47,  7, 39, 13, 45,  5, 37},
  25.                 {63, 31, 55, 23, 61, 29, 53, 21} 
  26.               }; // for dithering
  27.  
  28. static unsigned char black [8] = {128, 64, 32, 16, 8, 4, 2, 1};
  29. static unsigned char white [8] = {127, 191, 223, 239, 247, 251, 253, 254};
  30.  
  31. x1123x3d::x1123x3d() : plot3d()
  32.   {
  33.     row_array_allocated=FALSE;
  34.   }
  35.  
  36. x1123x3d::~x1123x3d()
  37.   {
  38.     if (row_array_allocated)
  39.       delete row_array;
  40.   }
  41.  
  42. int x1123x3d::display_initialized()
  43. //      Allocate and clear the page of graphics to be sent to the printer.
  44.     {
  45.       row_rec initialized_row_rec;
  46.  
  47.       if (row_array_allocated)
  48.         {
  49.            delete row_array;
  50.            row_array_allocated=FALSE;
  51.         }
  52.       for (int y_dot_num=0; y_dot_num < NUM_Y_PIXELS; y_dot_num++)
  53.         {
  54.           initialized_row_rec.column[y_dot_num][0]=(unsigned char) '\0';
  55.           initialized_row_rec.column[y_dot_num][1]=(unsigned char) '\0';
  56.           initialized_row_rec.column[y_dot_num][2]=(unsigned char) '\0';
  57.         }
  58.       row_array=new varray<row_rec>(initialized_row_rec,NUM_LINES,2);
  59.       row_array_allocated=(row_array->allocated());
  60.       return row_array_allocated;
  61.     }
  62.  
  63. void x1123x3d::pset(
  64.   int x,
  65.   int y,
  66.   int shading)  
  67. //      Use dithering to set the pixel at (x,y).
  68.     {
  69.       int     bit_num;
  70.       int     byte_num;
  71.       int     column_num;
  72.       int     on;
  73.       row_rec *row;
  74.       int     row_num;
  75.  
  76.       if (shading <= NUM_COLORS)
  77.         {
  78.           if (shading == NUM_COLORS)  // outline solution
  79.             if (pattern[x%8][y%8] < SOLUTION_GRAY)
  80.               on=1;
  81.             else
  82.               on=0;
  83.           else
  84.             if (pattern[x%8][y%8] < shading)
  85.               on=1;
  86.             else
  87.               on=0;
  88.           column_num=Y_DOT_MAX-y;
  89.           row_num=x/24;
  90.           row=row_array->vm_ptr((long) row_num);
  91.           bit_num=x-24*row_num;
  92.           byte_num=bit_num/8;
  93.           bit_num=bit_num-8*byte_num;
  94.           if (on)
  95.             row->column[column_num][byte_num]
  96.              =(row->column[column_num][byte_num] & white[bit_num]);
  97.           else
  98.             row->column[column_num][byte_num]
  99.              =(row->column[column_num][byte_num] | black[bit_num]);
  100.         }
  101.       return;
  102.     }
  103.  
  104. int x1123x3d::write_outfile(
  105.   char *file_name)
  106. //      Write the page of graphics to a file where it can later be copied
  107. // to the printer.  (The /B option *must* be used with the COPY command.)
  108.     {
  109.       char          bidirectional [3] = {'\033', 'U', '\000'};
  110.       char          default_lpi [2] = {'\033', '2'};
  111.       char          formfeed = '\014';
  112.       char          inches_per_line [3] = {'\033', '3', '\030'};
  113.       int           line_num;
  114.       unsigned char line_prefix [5] = {'\033', '*'};
  115.       FILE          *maze;
  116.       char          reset [2] = {'\033', '@'};
  117.       row_rec       *row;
  118.       char          space = ' ';
  119.       int           success;
  120.       char          unidirectional [3] = {'\033', 'U', '\001'};
  121.  
  122.       line_prefix[2]=(unsigned char) LINE_PREFIX_2;
  123.       if ((maze=fopen(file_name,"wb")) == NULL)
  124.         {
  125.           success=FALSE;
  126.           cerr << "Fatal error:  cannot open " << file_name << '\n';
  127.         }
  128.       else
  129.         {
  130.           success=(fwrite(reset,1,sizeof(reset),maze) == sizeof(reset));
  131.           if (success)
  132.             success=(fwrite(unidirectional,1,sizeof(unidirectional),maze)
  133.              == sizeof(unidirectional));
  134.           if (success)
  135.             success=(fwrite(inches_per_line,1,sizeof(inches_per_line),maze)
  136.              == sizeof(inches_per_line));
  137.           line_prefix[3]=(unsigned char) NUM_Y_PIXELS_MOD_256;
  138.           line_prefix[4]=(unsigned char) NUM_Y_PIXELS_DIV_256;
  139.           titillator_ptr=new titillator;
  140.           for (line_num=0; ((success) && (line_num < NUM_LINES));
  141.            line_num++)
  142.             {
  143.               titillator_ptr->titillate();
  144.               success=(fwrite(line_prefix,1,sizeof(line_prefix),maze)
  145.                == sizeof(line_prefix));
  146.               if (success)
  147.                 {
  148.                   row=row_array->vm_ptr((long) line_num);
  149.                   success
  150.                    =(fwrite(&((*row).column[0][0]),1,3*NUM_Y_PIXELS,maze)
  151.                    == 3*NUM_Y_PIXELS);
  152.                 }
  153.             }
  154.           delete titillator_ptr;
  155.           if (success)
  156.             success=(fwrite(default_lpi,1,sizeof(default_lpi),maze)
  157.              == sizeof(default_lpi));
  158.           if (success)
  159.             success=(fwrite(bidirectional,1,sizeof(bidirectional),maze)
  160.              == sizeof(bidirectional));
  161.           if (success)
  162.             success=(fwrite(&space,1,sizeof(space),maze) == sizeof(space));
  163.           if (success)
  164.             success=(fwrite(&formfeed,1,sizeof(formfeed),maze)
  165.              == sizeof(formfeed));
  166.           if (success)
  167.             success=(fwrite(&formfeed,1,sizeof(formfeed),maze)
  168.              == sizeof(formfeed));
  169.           if (! success)   
  170.             cerr << "Fatal error:  cannot write result.\n" << '\n';
  171.           fclose(maze);
  172.         }
  173.       return success;
  174.     }
  175.